package com.example.yunhailutong.zhongtan.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.app.App;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.example.yunhailutong.zhongtan.entity.ChanPinFenLeiEntity;
import com.example.yunhailutong.zhongtan.entity.GangChangEntity;
import com.example.yunhailutong.zhongtan.net.PersionBiz;
import com.example.yunhailutong.zhongtan.util.Utils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import fj.dropdownmenu.lib.concat.DropdownI;
import fj.dropdownmenu.lib.pojo.DropdownItemObject;
import me.leefeng.promptlibrary.PromptButton;
import me.leefeng.promptlibrary.PromptButtonListener;
import me.leefeng.promptlibrary.PromptDialog;

/**
 * Created by yunhailutong on 2018/4/26.
 */

public class ActivityNewDingDan extends ActivityBase implements DropdownI.SingleRow {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.gangchang)
    TextView gangchang;
    @BindView(R.id.update_register)
    Button updateRegister;
    @BindView(R.id.cailiao)
    TextView cailiao;
    @BindView(R.id.num)
    EditText num;
    @BindView(R.id.car_no)
    EditText carNo;
    @BindView(R.id.callman)
    EditText callman;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.mask)
    EditText mask;
    private String gangchangid = "";
    private String cailiaoid = "";
    private PromptDialog chanpinDialog;
    private PromptDialog gangchangDialog;
    private ChanPinFenLeiEntity chanPinFenLeiEntity;
    private GangChangEntity gangChangEntity;
    private String dingDanId;

    @Override
    protected void addListener() {

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        cailiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (chanPinFenLeiEntity == null) {
                    disPlay("请先选择钢厂");
                } else {
                    initChanPin(chanPinFenLeiEntity);
                }
            }
        });
        gangchang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (gangChangEntity == null) {
                    disPlay("请等待钢厂列表加载完成");
                } else {
                    initGangChang(gangChangEntity);
                }
            }
        });
    }

    @Override
    protected void initActivity(Bundle savedInstanceState) {
        ButterKnife.bind(this);
    }

    @Override
    protected int contentViewById() {
        return R.layout.activity_new_dingdan;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        Map<String, String> params = new HashMap<>();
        params.put("index", "1");
        params.put("length", "12");
        //获取钢厂列表
        new PersionBiz().GetZiXunList(params);
        showWaitingDialog();
        dingDanId = getIntent().getStringExtra("id");
        if(dingDanId == null){
            title.setText("创建订单");
            updateRegister.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!Utils.isMobileNO(phone.getText().toString())) {
                        disPlay("请输入有效的手机号");
                    } else if (callman.getText().toString().equals("")) {
                        disPlay("联系人不能为空");
                    } else if (!Utils.isCarNo(carNo.getText().toString())) {
                        disPlay("请输入正确的车牌号");
                    }else if (num.getText().toString().equals("")) {
                        disPlay("数量不能为空");
                    } else if (mask.getText().toString().equals("")) {
                        disPlay("备注不能为空");
                    }else if (gangchangid.equals("")) {
                        disPlay("请先选择钢厂");
                    } else if (cailiaoid.equals("")) {
                        disPlay("请先选择材料类别");
                    }   else {
                        Map<String, String> params = new HashMap<>();
                        //   params.put("username", userNameRegister.getText().toString());
                        params.put("phone", App.number);
                        params.put("token", App.token);
                        params.put("oi_pcId", cailiaoid);
                        params.put("oi_fmId", gangchangid);
                        params.put("oi_plateNumber", carNo.getText().toString());
                        params.put("oi_number", num.getText().toString());
                        params.put("oi_contact", callman.getText().toString());
                        params.put("oi_phone", phone.getText().toString());
                        params.put("oi_Remark", mask.getText().toString());
                        new PersionBiz().AddDingDan(params);
                        showWaitingDialog();
                    }
                }
            });
        }else{
            title.setText("修改订单");
            updateRegister.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!Utils.isMobileNO(phone.getText().toString())) {
                        disPlay("请输入有效的手机号");
                    } else if (callman.getText().toString().equals("")) {
                        disPlay("联系人不能为空");
                    } else if (carNo.getText().toString().equals("")) {
                        disPlay("车牌号不能为空");
                    }else if (num.getText().toString().equals("")) {
                        disPlay("数量不能为空");
                    } else if (mask.getText().toString().equals("")) {
                        disPlay("备注不能为空");
                    }else if (gangchangid.equals("")) {
                        disPlay("请先选择钢厂");
                    } else if (cailiaoid.equals("")) {
                        disPlay("请先选择材料类别");
                    }   else {
                        Map<String, String> params = new HashMap<>();
                        //   params.put("username", userNameRegister.getText().toString());
                        params.put("phone", "18765396874");
                        params.put("token", App.token);
                        params.put("oi_pcId", cailiaoid);
                        params.put("oi_fmId", gangchangid);
                        params.put("oi_plateNumber", carNo.getText().toString());
                        params.put("oi_number", num.getText().toString());
                        params.put("oi_contact", callman.getText().toString());
                        params.put("oi_phone", phone.getText().toString());
                        params.put("oi_Remark", mask.getText().toString());
                        params.put("oi_Remark", mask.getText().toString());
                        params.put("oi_id", dingDanId);
                        new PersionBiz().XiuGaiDingDan(params);
                        showWaitingDialog();
                    }
                }
            });
        }

    }

    @Subscribe
    public void log(OldsEvent.ChanPinFenLeiEvent msg) {
        dismissWaitingDialog();
        if (msg.what == -1) {
            disPlay(msg.obj.toString());
        }
        if (msg.what == 1) {
            chanPinFenLeiEntity = (ChanPinFenLeiEntity) msg.obj;
        }
    }
    @Subscribe
    public void log(OldsEvent.AddDingDanEvent msg) {
        dismissWaitingDialog();
        if (msg.what == -1) {
            disPlay(msg.obj.toString());
        }
        if (msg.what == 1) {
            disPlay("成功");
            finish();
        }
    }

    @Subscribe
    public void log(OldsEvent.ZiXunEvent msg) {
        dismissWaitingDialog();
        if (msg.what == -1) {
            disPlay(msg.obj.toString());
        }
        if (msg.what == 1) {
            gangChangEntity = (GangChangEntity) msg.obj;
        }
    }

    private void initChanPin(final ChanPinFenLeiEntity entity) {
        chanpinDialog = new PromptDialog(this);

        //可创建android效果的底部Sheet选择，默认IOS效果，sheetCellPad=0为Android效果的Sheet
        //                promptDialog.getAlertDefaultBuilder().sheetCellPad(0).round(0);
        //设置按钮的特点，颜色大小什么的，具体看PromptButton的成员变量
        PromptButton cancle = new PromptButton("取消", null);
        cancle.setTextColor(Color.parseColor("#0076ff"));
        //设置显示的文字大小及颜色
        chanpinDialog.getAlertDefaultBuilder().textColor(Color.GRAY);

        int length;
        if (entity.getData().size() < 11) {
            length = entity.getData().size() + 1;
        } else {
            length = 12;
        }
        PromptButton[] lists = new PromptButton[length];
        for (int i = 0; i < length; i++) {
            if (i == 0) {
                lists[i] = cancle;
            } else {
                final int finalI = i;
                String name = entity.getData().get(i - 1).getName();
                lists[i] = new PromptButton(name, new PromptButtonListener() {
                    @Override
                    public void onClick(PromptButton button) {
                        cailiaoid = String.valueOf(entity.getData().get(finalI - 1).getId());
                        cailiao.setText(entity.getData().get(finalI - 1).getName());
                    }
                });
            }
        }
        chanpinDialog.showAlertSheet("", true, lists);
    }

    private void initGangChang(final GangChangEntity entity) {
        gangchangDialog = new PromptDialog(this);

        //可创建android效果的底部Sheet选择，默认IOS效果，sheetCellPad=0为Android效果的Sheet
        //                promptDialog.getAlertDefaultBuilder().sheetCellPad(0).round(0);
        //设置按钮的特点，颜色大小什么的，具体看PromptButton的成员变量
        PromptButton cancle = new PromptButton("取消", null);
        cancle.setTextColor(Color.parseColor("#0076ff"));
        //设置显示的文字大小及颜色
        gangchangDialog.getAlertDefaultBuilder().textColor(Color.GRAY);

        int length;
        if (entity.getData().size() < 11) {
            length = entity.getData().size() + 1;
        } else {
            length = 12;
        }
        PromptButton[] lists = new PromptButton[length];
        for (int i = 0; i < length; i++) {
            if (i == 0) {
                lists[i] = cancle;
            } else {
                final int finalI = i;
                String name = entity.getData().get(i - 1).getName();
                lists[i] = new PromptButton(name, new PromptButtonListener() {
                    @Override
                    public void onClick(PromptButton button) {
                        gangchangid = String.valueOf(entity.getData().get(finalI - 1).getId());
                        gangchang.setText(entity.getData().get(finalI - 1).getName());
                        //获取钢厂ID后在查询对应的产品id
                        Map<String, String> params = new HashMap<>();
                        params.put("fmId", gangchangid);
                        new PersionBiz().GetChanPinFenLei(params);
                        showWaitingDialog();
                    }
                });

            }

        }
        gangchangDialog.showAlertSheet("", true, lists);
    }

    @Override
    public void onSingleChanged(DropdownItemObject dropdownItemObject) {

    }

}
